import { ElMessageBox, ElLoading } from "element-plus"
import store from "../store"



class Clipboarder {
    /**
     * 功能: 获取剪切板的文本
     * 
     */

    static async readText() {
        return await navigator.clipboard.readText()
    }

    static async writeText(data) {
        return await navigator.clipboard.writeText(data)
    }

    // static _func() {
    //     Clipboarder.readText().then((value) => {
    //         ref_obj.value = value
    //         ShowTip.successBox(`${pre_text}：\n${value}`, close_time_ms)
    //     })
    // }

    // static onCopyListenerAndShowTip(ref_obj, close_time_ms, pre_text = '复制的内容为') {
    //     Clipboarder.readText().then((value) => {
    //         ref_obj.value = value
    //         ShowTip.successBox(`${pre_text}：\n${value}`, close_time_ms)
    //     })
    //     document.addEventListener('copy', Clipboarder._func)

    // }

    // static async offCopyListener() {

    // }

}



class TipType {
    static success = `success`
    static info = 'info'
    static error = 'error'
    static warning = 'warning'
}

class ShowTip {

    static async closeBox() {
        return await ElMessageBox.close()
    }

    static async messageBox({message, type,
        title = '提示', confirmButtonText = '确定', cancelButtonText = '取消',
        showClose = true, showCancelButton = false, showConfirmButton = true,
        center = true, close_time_ms = null}) {
        if(close_time_ms){
            setTimeout(() => {
                ElMessageBox.close()
            }, close_time_ms)
        }
        return await ElMessageBox.confirm(message, title, {
            type: type,
            confirmButtonText: confirmButtonText,
            showCancelButton,
            showConfirmButton,
            center,
            cancelButtonText,
            showClose,
        })
    }

    static async successBox(message, close_time_ms = null, showConfirmButton = true, showCancelButton = false , center  = true) {
        let type = TipType.success
        return await ShowTip.messageBox({
            message, type, showCancelButton,showConfirmButton,  close_time_ms
        })
    }

    static async errorBox(message, close_time_ms = null, showConfirmButton = true, showCancelButton = false) {
        let type = TipType.error
        return await ShowTip.messageBox({message, type, showConfirmButton, showCancelButton, close_time_ms})
    }

    static async warningBox(message, close_time_ms = null, showConfirmButton = true, showCancelButton = false) {
        let type = TipType.warning
        return await ShowTip.messageBox({message ,type , showConfirmButton, showCancelButton, close_time_ms})
    }

    static async infoBox(message, close_time_ms = null, showConfirmButton = true, showCancelButton = false) {
        let type = TipType.info
        return await ShowTip.messageBox({message, type, showConfirmButton, showCancelButton ,close_time_ms})
    }

    static showLoading(text = "", fullscreen = true) {
        ElLoading.service({
            text,
            fullscreen
        })
    }

    static hideLoading() {
        ElLoading.service().close()
    }
}


const switchEmptyByData = (data) => {
    if(data && data.length && data.length > 0)
        store.commit('hidden', 'empty')
    else
        store.commit('show', 'empty')
}

export {
    Clipboarder,
    ShowTip,
    switchEmptyByData
}



